/**
 * @(#) ProfileProxy.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 인사기록 조회 - Proxy
 * 				인사기록을 조회하는 Model Logic 
 */
package com.skt.common_hr.myprofile.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.myprofile.MyProfileFacade;
	import com.skt.common_hr.myprofile.model.vo.ProfileVO;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class ProfileProxy extends CommonProxy
	{
		public static const NAME:String = "ProfileProxy";
		
		public var profile:ProfileVO;


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ProfileProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			loadData("Z_HR_0008", param, result, fault); 
		}
		
		
		// 혈액형
		public function getBloodList(setFunc:Function):void
		{
			var obj:Object = {};
			loadData("Z_HR_P001", obj, getBloodListResult, fault, setFunc);
		}
		public function getBloodListResult(event:ResultEvent):void
		{
			var bloodList:ArrayCollection = new ArrayCollection();
			if(retCode==0) {
				bloodList = event.result.T_CODE_BLOOD;
				event.result.func(bloodList);
			}
			else {
				//Alert.show(retText);
				bloodList = new ArrayCollection();
				event.result.func(bloodList);
			}
		}		
		
		// 양력 음력 구분
		public function getSolarLunarList(setFunc:Function):void
		{
			var obj:Object = {};
			loadData("Z_HR_P002", obj, getSolarLunarListResult, fault, setFunc);
		}
		public function getSolarLunarListResult(event:ResultEvent):void
		{
			var SolarLunarList:ArrayCollection = new ArrayCollection();
			if(retCode==0) {
				SolarLunarList = event.result.T_CODE_GUBUN;
				event.result.func(SolarLunarList);
			}
			else {
				//Alert.show(retText);
				SolarLunarList = new ArrayCollection();
				event.result.func(SolarLunarList);
			}
		}			
		
		public function sendTallSave(vsId:String, TALL:String, WEIGHT:String, BLOOD:String, setFunc:Function):void
		{
			var tables:Object = new Object();
			var table:Array = new Array();
			var params:Object = new Object();
			
			params["I_ACTIO"] 	= 'MOD';
			params["I_PERNR"] 	= Variables.myPernr;
			
			var obj:Object = {};
			if(vsId == 'vsTall') {
				obj.TALL 	= TALL;	
			} else if(vsId == 'vsWEIGHT') {
				obj.WEIGHT 	= WEIGHT;	
			} else if(vsId == 'vsBLOODT') {
				obj.BLOOD 	= BLOOD;	
			}
			table.push(obj);
			tables["ITAB"] = table;
			
			saveData("Z_HR_P001", params, tables, sendTallSaveResult, fault, setFunc);
		}
		public function sendTallSaveResult(event:ResultEvent):void
		{
			if(retCode==0) {
				event.result.func();
			}			
			else {
				Alert.show(retText);
			}
		}

		public function sendSolarLunarSave(vsId:String, GUBUN:String, FAMDT:String, GBDAT:String, setFunc:Function):void
		{
			var tables:Object = new Object();
			var table:Array = new Array();
			var params:Object = new Object();
			
			params["I_ACTIO"] 	= 'MOD';
			params["I_PERNR"] 	= Variables.myPernr;
			
			var obj:Object = {};
			if(vsId == 'vsGBDAT') {
				obj.GUBUN 	= GUBUN;				
				obj.GBDAT 	= GBDAT.replace(/-/g,'');				
			} else if(vsId == 'vsMARRIAGE') {
				obj.FAMDT 	= FAMDT.replace(/-/g,'');
			} 			
			
			
			
			table.push(obj);
			tables["ITAB"] = table;
			
			saveData("Z_HR_P002", params, tables, sendSolarLunarSaveResult, fault, setFunc);
		}
		public function sendSolarLunarSaveResult(event:ResultEvent):void
		{
			if(retCode==0) {
				event.result.func();
			}			
			else {
				Alert.show(retText);
			}
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				profile = parseProfile(event.result.RESULT[0]);
				facade.sendNotification(MyProfileFacade.LOAD_PROFILE_COMPLETE, this);
			}
			else {
				profile = new ProfileVO();
				facade.sendNotification(MyProfileFacade.LOAD_PROFILE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			profile = new ProfileVO();
			facade.sendNotification(MyProfileFacade.LOAD_PROFILE_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseProfile(retObj:Object):ProfileVO
		{
			var vo:ProfileVO = new ProfileVO();
			vo.PERNR		= StringUtil.trimFront(retObj["PERNR"], "0");
			vo.ENAME		= retObj["ENAME"];
			vo.PHOTO_URL	= retObj["PHOTO_URL"];
			if(vo.PHOTO_URL.length<1) {
				vo.PHOTO_URL = "assets/images/noimage.swf";
			}
			trace("phoho_url = " + vo.PHOTO_URL);
			vo.TEL_HOME		=retObj["TEL_HOME"];			// 집전화
			vo.TEL_OFFICE	=retObj["TEL_OFFICE"];		// 회사전화
			vo.TEL_MOBILE1	=retObj["TEL_MOBILE1"];		// 이동전화 1
			vo.TEL_MOBILE2	=retObj["TEL_MOBILE2"];		// 이동전화 2
			vo.FAX			=retObj["FAX"];				// 팩스
			vo.EMAIL_IN		=retObj["EMAIL_IN"];			// 사내메일	
			vo.EMAIL_EX		=retObj["EMAIL_EX"];			// 사외메일
			vo.ORGEH		=retObj["ORGEH"];			// 조직 코드
			vo.ORGTX		=retObj["ORGTX"];			// 부서명
			vo.PERSK		=retObj["PERSK"];			// 직군코드
			vo.PTEXT		=retObj["PTEXT"];			// 직군
			vo.STELL		=retObj["STELL"];			// 직무 코드
			vo.STELLT		=retObj["STELLT"];			// 직무명
			vo.BAND			=retObj["BAND"];				// 밴드코드
			vo.BANDT		=retObj["BANDT"];			// 밴드명
			vo.IDATE		=DateUtil.stringToDateTime(retObj["IDATE"]);	// 입사일
			trace( vo.IDATE );
			vo.ODATE		=DateUtil.stringToDateTime(retObj["ODATE"]);			// 부서배치일
			vo.PDATE		=DateUtil.stringToDateTime(retObj["PDATE"]);// 승진기준일
			vo.ANKEY		=retObj["ANKEY"];			// 근무지코드
			vo.ANKEYT		=retObj["ANKEYT"];			// 근무지명
			vo.MASSG		=retObj["MASSG"];			// 입사경로 코드
			vo.MASSGT		=retObj["MASSGT"];			// 입사경로명

			vo.ZYEAR		= retObj["ZYEAR"];			// 연차
			vo.TALENT		= retObj["TALENT"];			// 당해년도 TR
			vo.JWTXT		= retObj["JWTXT"];			// 직위 			
			vo.AGE			= retObj["AGE"];			// 나이
			return vo;
		}
		
	}
}